package bit2;
import java.util.*;
class RecentCounter {
    Queue<Integer> q;
    public RecentCounter() {
        this.q=new LinkedList<>();
    }

    public int ping(int t) {
        q.add(t);
        while(q.peek()<t-3000){
            q.poll();
        }
        return q.size();
    }
}
public class bitMain3 {
    public static int getResult(int n) {
        // write code here
        return ysf(n,2);
    }
    public static int ysf(int n,int m){
        int tmp=n%m==0?n/m:n/m+1;
        if(tmp<=m+1){
            return (tmp-1)*m+1;
        }
        int n1=ysf(tmp,m+1);
        return (n1-2)*m+1;
    }
    public static ArrayList<Integer> asylum(int[][] ope) {
        // write code here
        ArrayList<Integer> one=new ArrayList<>();
        ArrayList<Integer> two=new ArrayList<>();
        for (int i = 0; i < ope.length; i++) {
            if (ope[i][0]==1){
                one.add(ope[i][0]*ope[i][1]);
            }
            else {
                if (ope[i][1]==1){
                    int size= one.size();
                    for(int j=0;j<size;j++){
                        if(one.get(j)>0){
                            two.add(one.remove(j));
                            break;
                        }
                    }
                }
                else if(ope[i][1]==0){
                    two.add(one.remove(0));
                }
                else {
                    int size= one.size();
                    for(int j=0;j<size;j++){
                        if(one.get(j)<0){
                            two.add(one.remove(j));
                            break;
                        }
                    }
                }
            }
        }
        return two;
    }
    public static void main(String[] args) {
//        System.out.println(getResult(55));
        int ope[][]={{1,1},{1,-1},{2,0},{2,-1}};
        ArrayList<Integer> one=asylum(ope);
        System.out.println(one);
    }
}
